package com.mmall.common;

import com.mmall.util.PropertiesUtil;
import redis.clients.jedis.*;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;

import java.util.ArrayList;

/**
 * 分布式连接池
 * @author TomDragon
 */
public class RedisShardedPool {

    private static ShardedJedisPool pool;
    private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.total", "20"));
    private static Integer maxIdle  = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle", "20")) ;
    private static Integer minIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.min.idele", "20"));
    private static Boolean testOnBorrow = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.min.idele", "20"));
    private static Boolean testOnReturn = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.min.idele", "20"));
    private static String redis1Ip = PropertiesUtil.getProperty("redis1.ip");
    private static Integer redis1Port = Integer.parseInt(PropertiesUtil.getProperty("redis1.port"));

    private static String redis2Ip = PropertiesUtil.getProperty("redis2.ip");
    private static Integer redis2Port = Integer.parseInt(PropertiesUtil.getProperty("redis2.port"));

    /*private static String redis3Ip = PropertiesUtil.getProperty("redis3.ip");
    private static Integer redis3Port = Integer.parseInt(PropertiesUtil.getProperty("redis3.port"));*/

    private static void initPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturn);
        config.setBlockWhenExhausted(true);
        JedisShardInfo info1 = new JedisShardInfo(redis1Ip,redis1Port,1000*2);
        JedisShardInfo info2 = new JedisShardInfo(redis2Ip,redis2Port,1000*2);
        /*JedisShardInfo info3 = new JedisShardInfo(redis3Ip,redis3Port,1000*2);*/

        ArrayList<JedisShardInfo> jedisShardInfoArrayList = new ArrayList<JedisShardInfo>(2);
        jedisShardInfoArrayList.add(info1);
        jedisShardInfoArrayList.add(info2);
        /*jedisShardInfoArrayList.add(info3);*/

        //  Hashing.MURMUR_HASH 对应一致性hash算法
        pool = new ShardedJedisPool(config, jedisShardInfoArrayList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);
    }

    static{
        initPool();
    }

    public static ShardedJedis getJedis(){
        return pool.getResource();
    }

    public static void returnBrokenResource(ShardedJedis jedis){
        pool.returnBrokenResource(jedis);
    }

    public static void returnResource(ShardedJedis jedis){
        pool.returnResource(jedis);
    }

    /*public static void main(String[] args) {
        ShardedJedis jedis = pool.getResource();
        for (int i =0; i < 10; i++){
            jedis.set("key-00"+i, "value-00"+i);
            if (i > 5){
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        returnResource(jedis);
        //临时调用，销毁连接池中的所有连接
        //pool.destroy();
        System.out.println("program is end");
    }*/

}
